package com.threeonemeeting.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.snda.lib.ui.MessageBox;
import com.snda.lib.util.FileHelper;
import com.threeonemeeting.Const;
import com.threeonemeeting.R;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.util.AppSettings;
import com.threeonemeeting.util.MiscUtil;

public class SettingActivity extends BaseTabActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_setting);
		init("系统设置");

		initNotifyBtn();
		initImageQualityBtn();
		initClearCacheBtn();
	}

	private void selectOpen(boolean bOpen) {
		Button btnOpen = (Button) findViewById(R.id.open_button);
		Button btnClose = (Button) findViewById(R.id.close_button);

		if (bOpen == true) {
			btnOpen.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_left_round_selected));
			btnClose.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_right_round_default));
		} else {
			btnOpen.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_left_round_default));
			btnClose.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_right_round_selected));
		}
	}

	private void initNotifyBtn() {
		// 检查开启和关闭
		Button btnOpen = (Button) findViewById(R.id.open_button);
		Button btnClose = (Button) findViewById(R.id.close_button);

		String strValue = MiscUtil.getPrefer(Const.Pref.PREFS_KEY_NOTIFY);
		if (strValue == null || strValue.equals("") == true) {
			strValue = "0";
		}

		if (strValue.equalsIgnoreCase("1") == true) {
			selectOpen(true);

		} else {
			selectOpen(false);
		}

		btnOpen.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				MiscUtil.setPrefer(Const.Pref.PREFS_KEY_NOTIFY, "1");
				selectOpen(true);
			}
		});

		btnClose.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				MiscUtil.setPrefer(Const.Pref.PREFS_KEY_NOTIFY, "0");
				selectOpen(false);
			}
		});
	}

	private void selectImageQuality(int nQuality) {
		Button btnHigh = (Button) findViewById(R.id.h_button);
		Button btnMiddle = (Button) findViewById(R.id.m_button);
		Button btnLow = (Button) findViewById(R.id.l_button);

		if (nQuality == 2) {
			btnHigh.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_left_round_selected));
			btnMiddle.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_no_round_default));
			btnLow.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_right_round_default));
		} else if (nQuality == 1) {
			btnHigh.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_left_round_default));
			btnMiddle.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_no_round_selected));
			btnLow.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_right_round_default));
		} else {
			btnHigh.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_left_round_default));
			btnMiddle.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_no_round_default));
			btnLow.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.btn_style_red_right_round_selected));

		}
	}

	private void initImageQualityBtn() {

		Button btnHigh = (Button) findViewById(R.id.h_button);
		Button btnMiddle = (Button) findViewById(R.id.m_button);
		Button btnLow = (Button) findViewById(R.id.l_button);

		String strValue = MiscUtil
				.getPrefer(Const.Pref.PREFS_KEY_IMAGE_QUALITY);
		if (strValue == null || strValue.equals("") == true) {
			strValue = "0";
		}
		selectImageQuality(Integer.parseInt(strValue));

		btnHigh.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				MiscUtil.setPrefer(Const.Pref.PREFS_KEY_IMAGE_QUALITY, "2");
				selectImageQuality(2);
			}
		});

		btnMiddle.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				MiscUtil.setPrefer(Const.Pref.PREFS_KEY_IMAGE_QUALITY, "1");
				selectImageQuality(1);
			}
		});

		btnLow.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				MiscUtil.setPrefer(Const.Pref.PREFS_KEY_IMAGE_QUALITY, "0");
				selectImageQuality(0);
			}
		});
	}

	private void initClearCacheBtn() {

		Button btnClear = (Button) findViewById(R.id.clear_button);
		btnClear.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String strImagePath = AppSettings
						.getImagePath(getApplicationContext());
				FileHelper.delFolder(strImagePath);

				MessageBox.showToast(SettingActivity.this, "缓存图片清除成功");
			}
		});
	}
}
